import pyecharts.options as opts
from pyecharts.charts import Line, Kline
import numpy as np # 导入 numpy 模块




def chart(data, start_date, end_date):
    # 转换为 list
    names = np.array(data[2]).tolist()
    # 取第一个元素
    name = names[0]

    money = np.array(data[7]).tolist()
    dates = np.array(data[3]).tolist()

    week_name_list = dates
    high_temperature = money


    c = (
        Line()
        .add_xaxis(xaxis_data=week_name_list)
        .add_yaxis(
            series_name="开盘价",
            y_axis=high_temperature,
            markpoint_opts=opts.MarkPointOpts(
                data=[
                    opts.MarkPointItem(type_="max", name="最大值"),
                    opts.MarkPointItem(type_="min", name="最小值"),
                ]
            ),
            markline_opts=opts.MarkLineOpts(
                data=[opts.MarkLineItem(type_="average", name="平均值")]
            ),
        )
        .set_series_opts(
            areastyle_opts=opts.AreaStyleOpts(opacity=0.5),
            label_opts=opts.LabelOpts(is_show=False),
        )

        .set_global_opts(
            title_opts=opts.TitleOpts(title="股票开盘价走势", subtitle=name + "股票" + start_date + "到" + end_date + "的走势图"),
            tooltip_opts=opts.TooltipOpts(trigger="axis"),
            toolbox_opts=opts.ToolboxOpts(is_show=True),
            xaxis_opts=opts.AxisOpts(type_="category", boundary_gap=False),
        )
        .render_embed()
    )

    return c


def k(data, start_date, end_date):
    # 转换为 list
    names = np.array(data[2]).tolist()
    # 取第一个元素
    name = names[0]
    # 切片取 日期
    dates = np.array(data[3]).tolist()


    # print(data)

    # 从 dataframe 中取 7~10 列的数据写成 list
    k_data = np.array(data[[7, 8, 9, 10]]).tolist()

    c = (
        Kline()
        .add_xaxis(dates)
        .add_yaxis(
            name,
            k_data,
            itemstyle_opts=opts.ItemStyleOpts(
                color="#ec0000",
                color0="#00da3c",
                border_color="#8A0000",
                border_color0="#008F28",
            ),
        )
        .set_global_opts(
            xaxis_opts=opts.AxisOpts(is_scale=True),
            yaxis_opts=opts.AxisOpts(
                is_scale=True,
                splitarea_opts=opts.SplitAreaOpts(
                    is_show=True, areastyle_opts=opts.AreaStyleOpts(opacity=1)
                ),
            ),
            datazoom_opts=[opts.DataZoomOpts(type_="inside")],
            title_opts=opts.TitleOpts(title="Kline-ItemStyle"),
        )
        .render_embed()
    )
    return c







